<?php
error_reporting(0);
@header('Content-Type: text/html; charset=UTF-8');
@header('Content-Type: text/html; charset=UTF-8');
if(file_exists('install.lock')){
	exit('检测到您已安装过本源码，若需要重新安装，请删除该目录下的install.lock');
}
$do = isset($_GET['do']) ? $_GET['do'] : '0';
$mr_DB_QZ = "frame"; //默认数据库前缀

/*reference: 小照 */
function checkfunction($f, $m = false){
	if (function_exists($f)) {
		return '可用';
	} else {
		if ($m == false) {
			return '<不支持';
		} else {
			return '不支持';
		}
	}
}

function checkclass($f, $m = false){
	if (class_exists($f)) {
		return '可用';
	} else {
		if ($m == false) {
			return '不支持';
		} else {
			return '不支持';
		}
	}
}

function msg($content, $ret) {
    if ($ret == 1) {
        $ret = "history.go(-1);</script>";
    } else {
        $ret = "window.location.href='" . $ret . "';</script>";
    }
    exit("<script language='javascript'>alert('$content');$ret</script>");
}

function real_ip() {
    $ip = $_SERVER['REMOTE_ADDR'];
    if (isset($_SERVER['HTTP_CLIENT_IP']) && preg_match('/^([0-9]{1,3}\.){3}[0-9]{1,3}$/', $_SERVER['HTTP_CLIENT_IP'])) {
        $ip = $_SERVER['HTTP_CLIENT_IP'];
    } elseif (isset($_SERVER['HTTP_X_FORWARDED_FOR']) AND preg_match_all('#\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}#s', $_SERVER['HTTP_X_FORWARDED_FOR'], $matches)) {
        foreach ($matches[0] AS $xip) {
            if (!preg_match('#^(10|172\.16|192\.168)\.#', $xip)) {
                $ip = $xip;
                break;
            }
        }
    }
    return $ip;
}
?>
<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8" />
		<title>安装 - 小照PHP框架 专注WEB应用快速开发!</title>
		<link rel="icon" href="/favicon.ico" type="image/x-icon" />
		<meta name="description" content="" />
		<meta name="keywords" content="">
		<link rel="stylesheet" type="text/css" href="../layui/css/layui.css" />
		
		<style>
			body{
				margin: 0 auto;
				color: #2c3e50;
				/*
				text-align: center;
				*/
			}
			.container {
			  max-width: 580px;
			  padding: 50px;
			  margin: 0 auto;
			  text-align: center;
			}
		</style>
	</head>
	
	<body style="background-image: url(../layui/images/background.png);">
		<ul class="layui-nav" lay-filter="">
			<li class="layui-nav-item layui-this">
				<a href="#">首次初始化框架</a>
			</li>
			<li class="layui-nav-item">
				<a href="http://wpa.qq.com/msgrd?v=3&uin=2453162656&site=qq&menu=yes">联系作者</a>
			</li>
		</ul>
			
		<div class="container">
<?php if($do==0){ ?>
			<div class="layui-card">
				<div class="layui-card-header">环境检查</div>
				<div class="layui-card-body">
					<table class="layui-table">
						<tbody>
							<tr>
								<th>PHP版本支持(必须 7.0+)</th>
								<td><?php echo phpversion(); ?></td></tr>
							<tr>
								<th>抓取网页(必须 curl_exec())</th>
								<td><?php echo checkfunction( 'curl_exec', true); ?></td></tr>
							<tr>
								<th>读取文件(必须 file_get_contents())</th>
								<td><?php echo checkfunction( 'file_get_contents', true); ?></td></tr>
							<tr>
								<th>发送邮件(必须 fsockopen())</th>
								<td><?php echo checkfunction( 'fsockopen'); ?></td></tr>
							<tr>
								<th>Zip 解包和压缩(推荐 ZipArchive)</th>
								<td><?php echo checkclass( 'ZipArchive'); ?></td></tr>
							<tr>
								<th>写入文件(推荐 1/2)</th>
								<td><?php if (is_writable( './')) {echo '支持';}else{echo '不支持';} ?></td></tr>
							<tr>
								<th>写入文件(推荐 2/2)</th>
								<td><?php echo checkfunction( 'file_put_contents'); ?></td></tr>
							<tr>
								<th>ThinkPHP</th>
								<td>小照 PHP.Frame</td></tr>
							<tr>
								<th>上传附件限制</th>
								<td><?=ini_get('upload_max_filesize')?></td></tr>
							<tr>
								<th>执行时间限制</th>
								<td><?=ini_get('max_execution_time').'秒'?></td></tr>
							<tr>
								<th>剩余空间</th>
								<td><?=round((disk_free_space(".")/(1024*1024)),2)?>M</td></tr>
						</tbody>
					</table>
					<a href="?do=1" class="layui-btn layui-btn-fluid">下一步</a>
				</div>
			</div>
<?php }elseif($do==1){ 
if(phpversion() < '7.0'){
	msg('PHP版本低于7.0+, 无法继续安装！',1);
}
?>
			<div class="layui-card">
				<div class="layui-card-header">数据库配置</div>
				<div class="layui-card-body">
					<form action="?do=2" class="layui-form layui-form-pane" method="post">
						<div class="layui-form-item">
							<label class="layui-form-label">数据库地址</label>
							<div class="layui-input-block">
								<input type="text" name="DB_HOST" autocomplete="off" placeholder="请输入数据库地址" value="localhost" class="layui-input">
							</div>
						</div>
						<div class="layui-form-item">
							<label class="layui-form-label">数据库端口</label>
							<div class="layui-input-block">
								<input type="text" name="DB_PORT" autocomplete="off" placeholder="请输入数据库端口" value="3306" class="layui-input">
							</div>
						</div>
						<div class="layui-form-item">
							<label class="layui-form-label">数据库库名</label>
							<div class="layui-input-block">
								<input type="text" name="DB_NAME" autocomplete="off" placeholder="请输入数据库库名" class="layui-input">
							</div>
						</div>
						<div class="layui-form-item">
							<label class="layui-form-label">数据库用户名</label>
							<div class="layui-input-block">
								<input type="text" name="DB_USER" autocomplete="off" placeholder="请输入数据库用户名" class="layui-input">
							</div>
						</div>
						<div class="layui-form-item">
							<label class="layui-form-label">数据库密码</label>
							<div class="layui-input-block">
								<input type="text" name="DB_PWD" autocomplete="off" placeholder="请输入数据库密码" class="layui-input">
							</div>
						</div>
						<div class="layui-form-item">
							<label class="layui-form-label">数据库前缀</label>
							<div class="layui-input-block">
								<input type="text" name="DB_QZ" autocomplete="off" placeholder="请输入数据库前缀" value="<?=$mr_DB_QZ ?>" class="layui-input">
							</div>
						</div>
						<button type="submit" class="layui-btn layui-btn-fluid">下一步</button>
					</form>
				</div>
			</div>
<?php }elseif($do==2){ 
	if(!$_POST['DB_HOST'] || !$_POST['DB_PORT'] || !$_POST['DB_NAME'] || !$_POST['DB_USER'] || !$_POST['DB_PWD'] || !$_POST['DB_QZ']){
		msg('所有项都不能为空',1);
	}else{
		$conn = new mysqli($_POST['DB_HOST'].':'.$_POST['DB_PORT'], $_POST['DB_USER'], $_POST['DB_PWD'], $_POST['DB_NAME']);
		if(mysqli_connect_errno()){
			msg('数据库连接失败',1);
		}else{
			$conn->query("set names utf8");//设置数据库编码
			$data='<?php
/*数据库配置*/
$host = "'.$_POST['DB_HOST'].'"; //MYSQL主机
$port = '.$_POST['DB_PORT'].'; //MYSQL主机
$user = "'.$_POST['DB_USER'].'"; //MYSQL用户
$pwd ="'.$_POST['DB_PWD'].'"; //MYSQL密码
$dbname = "'.$_POST['DB_NAME'].'"; //数据库名

/*系统配置*/
$db_qz = "'.$_POST['DB_QZ'].'"; //数据表前缀
?>';
			file_put_contents('../config.php',$data);
			$sqls 	 = file_get_contents("install.sql");
			$sqls	 = str_replace('{DBQZ}', $_POST['DB_QZ'], $sqls);
			$explode = explode(';',$sqls);
			foreach ($explode as $_value) {
				$conn->query($_value.';');
			}
			if(mysqli_errno()){
				msg('导入数据表时错误，'.mysqli_errno(),1);
			}else{
				echo '
				<div class="layui-card">
					<div class="layui-card-header">网站配置</div>
					<div class="layui-card-body">
						<form action="?do=3" class="layui-form layui-form-pane" method="post">
							<div class="layui-form-item">
								<label class="layui-form-label">管理员账号(ID=1)</label>
								<div class="layui-input-block">
									<input type="text" name="user" autocomplete="off" placeholder="管理员账号" value="" class="layui-input">
								</div>
							</div>
							<div class="layui-form-item">
								<label class="layui-form-label">管理员密码</label>
								<div class="layui-input-block">
									<input type="pass" name="pwd" autocomplete="off" placeholder="管理员密码" value="" class="layui-input">
								</div>
							</div>
							<div class="layui-form-item">
								<label class="layui-form-label">网站名称</label>
								<div class="layui-input-block">
									<input type="text" name="sitename" autocomplete="off" placeholder="网站名称" value="小照PHP框架 专注WEB应用快速开发!" class="layui-input">
								</div>
							</div>
							<div class="layui-form-item">
								<label class="layui-form-label">站长ＱＱ</label>
								<div class="layui-input-block">
									<input type="text" name="kfqq" autocomplete="off" placeholder="站长QQ" class="layui-input">
								</div>
							</div>
							<button type="submit" class="layui-btn layui-btn-fluid">下一步</button>
						</form>
					</div>
				</div>
				';
			}
			$conn->close();
		}
	}
}elseif($do==3){
	$gl_user 	= isset($_POST['user'])?$_POST['user']:'admin';
	$gl_pwd 	= isset($_POST['pwd'])?$_POST['pwd']:'admin';
	$md5_pwd 	= md5($gl_pwd);
	$sitename	= isset($_POST['sitename'])?$_POST['sitename']:'小照PHP框架 专注WEB应用快速开发20年!';
	$kfqq		= isset($_POST['kfqq'])?$_POST['kfqq']:'2453162656';
	$date 		= date('y-m-d h:i:s');
	$ip			= real_ip();
	include_once '../config.php';
	if (isset($db_qz)) define('DBQZ', $db_qz);
	else define('DBQZ', 'frame');
	if(!isset($port))$port='3306';
	include_once("../includes/db.class.php");
	if(defined('SQLITE'))$DB = new DB($db_file);
	else $DB = new DB($host,$user,$pwd,$dbname,$port);
	saveSetting('name',$sitename);
	$DB->query("INSERT INTO `". DBQZ ."_user`(`uid`, `user`, `pwd`, `qq`, `active`, `ip`, `regtime`, `regip`) VALUES (1, '{$gl_user}', '{$md5_pwd}', '{$kfqq}', '1', '{$ip}', '{$date}', '{$ip}')");
	@file_put_contents('install.lock',$sitename.'.安装时间:'.$date);
?>
			<div class="layui-card">
				<div class="layui-card-header">安装成功</div>
				<div class="layui-card-body">
					<table class="layui-table">
						<tbody>
							<tr>
								<th>账号</th>
								<td><?=$gl_user ?></td>
							</tr>
							<tr>
								<th>密码</th>
								<td><?=$gl_pwd ?></td>
							</tr>
							<tr>
								<th>QQ号码</th>
								<td><?=$kfqq ?></td>
							</tr>
							<tr>
								<th>网站标题</th>
								<td><?=$sitename ?></td>
							</tr>
							<tr>
								<th>建成日期</th>
								<td><?=$date ?></td>
							</tr>
						</tbody>
					</table>
					<a href="../admin" class="layui-btn layui-btn-fluid">打开后台</a>
					<hr />
					<a href="../user" class="layui-btn layui-btn-fluid">打开前台</a>
				</div>
			</div>
<?php }elseif($do==4){ ?>
			<div class="layui-card">
				<div class="layui-card-header">卡片面板</div>
				<div class="layui-card-body">
					卡片式面板面板通常用于非白色背景色的主体内
					<br>从而映衬出边框投影
				</div>
			</div>
<?php } 
function saveSetting($k, $v){
	global $DB;
	$v = addslashes($v);
	return $DB->query("REPLACE INTO ".DBQZ."_config SET v='$v',k='$k'");
}

?>
		</div>

		<script src="../layui/layui.all.js"></script>
		<script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.min.js"></script>
		<!--您的Layui代码start-->
		<script>
		
		</script>
</body>
</html>